package com.yctan.base.entity;

import com.github.pagehelper.PageInfo;
import com.yctan.base.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public R() {
        put("status", 0);
        put("msg", "success");
    }

    public R set(String key, Object value) {
        this.put(key, value);
        return this;
    }

    public static R ok(HttpServletRequest request) {
        R r = new R();
        r.put("data", null);
        r.put("token", refreshToken(request));
        r.put("status", 200);
        return r;
    }

    public static R ok(Object data, HttpServletRequest request) {
        R r = new R();
        r.put("data", data);
        r.put("token", refreshToken(request));
        r.put("status", 200);
        return r;
    }

    public static R ok(Object data) {
        R r = new R();
        if (StringUtils.isBlank(data.toString())) {
            data = null;
        }
        r.put("data", data);
        r.put("status", 200);
        return r;
    }

    public static R okPage(List list, HttpServletRequest request) {
        R r = new R();
        r.put("token", refreshToken(request));
        PageInfo pageInfo = new PageInfo<>(list);
        r.put("data", pageInfo.getList());
        //当前页条数
        r.put("size", pageInfo.getSize());
        //每页条数
        r.put("pageSize", pageInfo.getPageSize());
        //当前页数
        r.put("pageNum", pageInfo.getPageNum());
        //总页数
        r.put("pages", pageInfo.getPages());
        //总记录数
        r.put("total", pageInfo.getTotal());

        r.put("status", 200);
        return r;
    }

    public static R ok(Object data, String msg) {
        R r = new R();
        r.put("data", data);
        r.put("status", 200);
        r.put("msg", msg);
        return r;
    }


    public static R error(String msg, HttpServletResponse response) {
        R r = new R();
        r.put("status", 255);
        r.put("msg", msg);
        response.setStatus(255);
        return r;
    }

    public static R error(int status, String msg, HttpServletResponse response) {
        R r = new R();
        r.put("status", status);
        r.put("msg", msg);
        response.setStatus(status);
        return r;
    }
    /**
     * 判断是否需要更新token
     *
     * @return
     */
    public static String refreshToken(HttpServletRequest request) {
        //获取token
        String token = request.getHeader(JwtUtils.header);
        if (StringUtils.isEmpty(token)) {
            token = request.getParameter(JwtUtils.header);
        }
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        //解析token
        Claims claims = JwtUtils.getTokenClaim(token);
        if (claims == null) {
            return null;
        }
        if (JwtUtils.timeRemaining(token)) {
            //需要更新
            //获取用户id
            Admin admin = new Admin();
            Supplier supplier = new Supplier();
            supplier.setId(Integer.parseInt(claims.get("supplier").toString()));
            admin.setSupplier(supplier);
            admin.setId(Integer.parseInt(claims.get("id").toString()));
            admin.setUserType(Integer.parseInt(claims.get("userType").toString()));
            String newToken = JwtUtils.createToken(admin);
            return newToken;
        }
        return null;
    }
}
